package com.baturu.vin.hyundai.transformer;

import com.baturu.kit.kit.function.SafeFunction;
import com.baturu.vin.dto.EPCPartsImageDTO;
import com.baturu.vin.dto.superEPC.SEPCGroupDTO;
import com.baturu.vin.dto.superEPC.SEPCPartsDTO;
import com.baturu.vin.dto.superEPC.SEPCSearchGroupDTO;
import com.baturu.vin.hyundai.constant.HyundaiKiaCommonConstant;
import com.baturu.vin.hyundai.dal.entity.SEPCHyundaiSubGroupEntity;
import com.baturu.vin.hyundai.dto.KiaPartsDTO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: chenjiahao
 * @Time: 2019/7/9
 */
@Slf4j
public class HyundaiKiaSEPCGroupTransformer {
    public static final SafeFunction<SEPCGroupDTO, SEPCHyundaiSubGroupEntity> SEPC_GROUP_TO_HYUNDAI_KIA_GROUP = input -> {
        if (CollectionUtils.isEmpty(input.getEpcPartsImageDTOList())) {
            log.warn("现代起亚imageid为空");
            return null;
        }
        String subGroupCode = input.getEpcPartsImageDTOList().get(0).getImageCode();
        if (StringUtils.isBlank(subGroupCode)) {
            return null;
        }
        String code = StringUtils.substring(subGroupCode, 0, subGroupCode.length() - 2);
        String imageXY = StringUtils.substring(subGroupCode, subGroupCode.length() - 2);
        String[] imageXYList = imageXY.split("");
        //图片偏移量默认是两位
        if (imageXYList.length != HyundaiKiaCommonConstant.IMAGEXY_COUNT) {
            log.warn("现代起亚偏移量有错:{}", subGroupCode);
            return null;
        }
        return SEPCHyundaiSubGroupEntity.builder()
                .groupCode(code)
                .imageX(imageXYList[0])
                .imageY(imageXYList[1])
                .build();
    };

    public static final SafeFunction<KiaPartsDTO, SEPCSearchGroupDTO> PARTS_TO_SEARCH_GROUP = input -> {
        EPCPartsImageDTO epcPartsImageDTO = new EPCPartsImageDTO();
        epcPartsImageDTO.setImageCode(input.getGroupCode() + input.getImgX() + input.getImgY());
        return SEPCSearchGroupDTO.builder()
                .partsDTO(SEPCPartsDTO.builder()
                        .partsCode(input.getFinalPartsCode())
                        .epcName(input.getFinalPartName())
                        .epcDesc(input.getEpcRemark())
                        .build())
                .groupDTO(SEPCGroupDTO.builder().mainGroup(input.getClassify())
                        .subGroup(input.getGroupCode() + " " + input.getGroupName())
                        .subGroupCode(input.getGroupCode() + input.getImgX() + input.getImgY())
                        .parentGroup(SEPCGroupDTO.builder().mainGroup(input.getClassify()).build())
                        .epcPartsImageDTOList(Lists.newArrayList(epcPartsImageDTO))
                        .build())
                .build();
    };

    public static final SafeFunction<KiaPartsDTO, SEPCHyundaiSubGroupEntity> PARTS_TO_GROUP_ENTITY = input ->
            SEPCHyundaiSubGroupEntity.builder()
                    .imageY(input.getImgY())
                    .imageX(input.getImgX())
                    .imageUrl(input.getFinalPartsImageUrl())
                    .groupCode(input.getGroupCode())
                    .groupName(input.getGroupName())
                    .build();
}
